//获取应用实例
var app = getApp();
var util = require('../../util.js');
// 初始化数据
var init = {

  // 数据
  data: {

    // 数据加载
    loading: false,

    // 加载提示
    loadTips: '',
    page:1,//当前页码
    totalpage:100,//总页码
    content:[],
    status:0,
    first:true

  },

  // 页面加载
  onLoad: function (options) {
    this.setData({
      options: options
    });
    console.log('onLoad');
    this.getData();
    if(options.from=='inside'){
      this.setData({
        act:'sys'
      })
    }else{
      this.setData({
        act: 'broadcast'
      })
    }
  },

  // 获取数据
  getData: function () {
    var that = this;
    if(that.data.page<=that.data.totalpage){
      app.getLoginSession(function (session) {
        wx.request({

          // 请求地址
          url: app.api + "/wx_log_test/msg/"+that.data.act,

          // 请求参数
          data: {
            token: session,
            page: that.data.page,
            perpage: 10,
            isread:that.data.status
          },

          // 请求成功
          success: function (res) {
            var res = res.data;
            if (res.msg == "OK") {
              var data = res.data;
              if (data.broadcastPage.totalPages == 0){
                that.setData({
                  totalpage: data.broadcastPage.totalPages,
                  page:++that.data.page,
                  loading:false,
                  loadTips:'暂无系统消息'
                })
              } else{
                that.setData({
                  totalpage: data.broadcastPage.totalPages,
                  page: ++that.data.page,
                  loading: false,
                  content: that.data.content.concat(that.deallist(data.broadcastPage.content)),
                  data:data
                })
                if (data.broadcastPage.totalPages==that.data.page-1){
                  that.setData({
                    loadTips: '暂无更多消息',
                  })
                }
              }
            }else{
              that.setData({
                loading: false,
                loadTips: '接口请求错误'
              })
            }
          },fail:function(res){
            that.setData({
              loading: true,
              errorTips: '接口请求错误'
            })
          }
        })
      })
    }
    
  },
  tap:function(res){
    var that = this;
    that.setData({
      status:res.currentTarget.dataset.status,
      // 数据加载
      loading: false,

      // 加载提示
      loadTips: '',
      page: 1,//当前页码
      totalpage: 100,//总页码
      content: [],
    })
    console.log('tap');
    that.getData();
  },
  deallist(list) {
    if (list && Array.isArray(list)) {
      list.forEach((item) => {
        item.createdOn = util.formatDate(new Date(item.createdOn), 'YYYY-MM-dd hh:mm:ss')
      })
      return list
    }
  },
  //上拉加载更多
  lower:function(res){
    if (this.data.first==true){
      this.setData({
        first:false
      })
    } else {
      console.log('lower');
      this.getData();
    }
  },
  onShow:function(res){
    // console.log('onShow');
    // this.getData();
  }

};

// 注册页面
Page(init);
